package com.mall.product.service.impl;


import com.mall.common.utils.BeanUtilKit;
import com.mall.common.utils.Constant;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.CategoryService;
import com.mall.product.vo.AttrVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.AttrDao;
import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;

    @Autowired
    private CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId,String attrType) {
        QueryWrapper<AttrEntity> warpper = new QueryWrapper<AttrEntity>();
        String key = (String) params.get(Constant.KEY);

        if("base".equalsIgnoreCase(attrType)){
            warpper.eq("attr_type",1);
        }else{
            warpper.eq("attr_type",0);
        }

        if(StringUtils.isNotBlank(key)){
            warpper.like("attr_name",key);

        }
        if(categoryId !=0 ){
            warpper.eq("catelog_id",categoryId);
        }

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),warpper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveAttrRelation(AttrVO attrVO) {
        AttrEntity attrEntity = new AttrEntity();
        //保存attr
        BeanUtils.copyProperties(attrVO,attrEntity);
        this.save(attrEntity);
        //保存属性关联group分组表
        if(attrVO.getAttrType() == 1){
            AttrAttrgroupRelationEntity attrGroupEntity = new AttrAttrgroupRelationEntity();
            attrGroupEntity.setAttrGroupId(attrVO.getAttrGroupId());
            attrGroupEntity.setAttrId(attrEntity.getAttrId());
            attrgroupRelationService.save(attrGroupEntity);
        }

    }

    @Override
    public AttrVO getAttrVO(Long attrId) {
        AttrEntity attr = this.getById(attrId);
        AttrVO attrVO =new AttrVO();
        BeanUtils.copyProperties(attr,attrVO);

        if(attr.getAttrType() == 1){
            AttrGroupEntity  attrGroup = attrgroupRelationService.getAttrGroupNameByAttrId(attrId);
           if(!ObjectUtils.isEmpty(attrGroup)){
               attrVO.setGroupName(attrGroup.getAttrGroupName());
               attrVO.setAttrGroupId(attrGroup.getAttrGroupId());
           }
        }
        attrVO.setCatelogPath(categoryService.getCatelogPath( attr.getCatelogId()));

        return attrVO;
    }

    @Override
    public List<AttrVO> getAttrVOByGroupId(Long groupId) {
       List<AttrEntity> entityList = this.baseMapper.getAttrVOByGroupId(groupId);
        return BeanUtilKit.copyObjectList(entityList,AttrVO.class);
    }

}